What is jest-haste-map?
The jest-haste-map npm package is a utility for building a Haste map, which is a mapping from module names to file paths. It is used by Jest to quickly resolve module dependencies for tests by keeping an in-memory map of all available modules. It can handle duplicate module names and provides a way to query the map for a specific module.
Building a Haste Map
This code sample demonstrates how to create a new HasteMap instance with a configuration object, build the map, and then use it to get the module name for a specific file.
const HasteMap = require('jest-haste-map');
const config = {
// These options are required:
roots: ['/path/to/project'],
extensions: ['js', 'json'],
platforms: ['ios', 'android'],
// You can provide additional options:
computeSha1: true,
// other options...
};
const hasteMap = new HasteMap(config);
hasteMap.build().then(map => {
console.log(map.hasteFS.getModuleName('/path/to/project/file.js'));
});
Querying the Haste Map
This code sample shows how to query the built Haste map for the path of a module given its name, platform, and whether it supports the native platform.
hasteMap.build().then(map => {
const modulePath = map.moduleMap.getModule('moduleName', 'platform', 'supportsNativePlatform');
console.log(modulePath);
});